$(function(){

    //报表类型
    var type,year,month,date,textName,fuText;
    var search = function ()
    {	
    	$('.reportMonth').hide();
    	type = $('#type').val();
        year = $('#year').val();
        month = $('#month').val();
    	date = year + "-"+month;
    	textName = $("#type[name=type]").find("option:selected").text();
    	url = "/modules/operatingStatistics/overdueAnalysis.html?type=" + type + "&year=" + year;
    	ajaxUrl(url,type,textName);
    }

	var ajaxUrl = function (url,type,textName){
		$.ajax({
			url:url + "&randomTime=" + (new Date()).getTime(),
			type:"get",
			dataType:"json",
			cache:false,
			success:function(data){
				var option;
				var sums = 0;
				for(var i=0;i<data.counts.length;i++)
				{
					sums = sums+data.counts[i];
					if(sums == 0)
					{
						fuText = '暂无数据';
					}
					else
					{
						fuText = '';
					}
				}
				if(type == 0)//type0
				{
					for(var i in data.sums)
					{
						data.sums[i] = data.sums[i]*10000/100000000;
					}
					option = {
					    title : {
					        text: textName,
					        x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
					    },
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					    	show : true,
					    	orient : 'horizontal',
					        x : 'center',
					        y : 'bottom',
					        selectedMode : true,
					        data:['人数（人）', '金额（万元）']
					    },
					    xAxis : [
					        {
					            type : 'category',
					            data : data.dates
					            
					        }
					    ],
					    yAxis : [
					        {
					           type : 'value',
					           boundaryGap : [0, 0.01],
					           axisLabel : {
					                formatter: '{value} 万元'
					            }
					        }
					    ],
					    series : [
					        {
					            name:'人数（人）',
					            type:'bar',
					            data:data.counts
					        },
					        {
					            name:'金额（万元）',
					            type:'bar',
					            data:data.sums
					        }
					    ]
					};
				}
				else if(type == 1)//type1
				{
					option = {
						title : {
					        text: textName,
					        x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
					    },
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					    	show : true,
					        x : 'center',
					        y: 'bottom',
					        selectedMode : true,
					        data:['逾期人数', '同比值']
					    },
					    xAxis : [
					        {
					            type : 'category',
					            data : data.dates
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value',
					            axisLabel : {
					                formatter: '{value} 人'
					            }
					        },
					        {
					            type : 'value',
					            name : '同比值',
					            axisLabel : {
					                formatter: '{value} %'
					            }
					        }
					    ],
					    series : [
					        {
					            name:'逾期人数',
					            type:'bar',
					            data:data.counts
					        },
					        {
					            name:'同比值',
					            type:'line',
					            yAxisIndex: 1,
					            data:data.ratios
					        }
					    ]
					};                    
				}
				else if(type == 2)
				{
					if(data.counts)
					{
						for(var i in data.counts)
						{
							data.counts[i] = data.counts[i]*10000/100000000;
						}
					}
					option = {
						title : {
					        text: textName,
					        x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext: fuText
					    },
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					    	show : true,
					        x : 'center',
					        y: 'bottom',
					        selectedMode : true,
					        data:['逾期金额（万元）', '同比值']
					    },
					    xAxis : [
					        {
					            type : 'category',
					            data : data.dates
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value',
					            axisLabel : {
					                formatter: '{value} 万元'
					            }
					        },
					        {
					            type : 'value',
					            name : '同比值',
					            axisLabel : {
					                formatter: '{value} %'
					            }
					        }
					    ],
					    series : [
					        {
					            name:'逾期金额（万元）',
					            type:'bar',
					            data:data.counts
					        },
					        {
					            name:'同比值',
					            type:'line',
					            yAxisIndex: 1,
					            data:data.ratios
					        }
					    ]
					};
					                    
					                    
				}
				// 基于准备好的dom，初始化echarts图表
		        var myChart = echarts.init(document.getElementById('investTablea')); 
		        // 为echarts对象加载数据 
		        myChart.setOption(option); 
			}
		});
	};
		var changBtn = $('.investorsTabs .tabs-header ul li a:eq(0)');
		changBtn.click(function(){
			search();//初始化加载
		});
		search();
	    $("#type,#year,#month").change(function(){
			search();
	    });

	//按钮二
	var searchb = function()
    {
    	type = $('#basicType').val();
        year = $('#basicYear').val();
        month = $('#basicMonth').val();
    	date = year + "-"+month;
    	$('.reportMonth').show();
    	if(type == 0)
	    {
	    	url = "/modules/operatingStatistics/borrowOverdueBaseInfo.html?type=" + type + "&date=" + date;
	    	ajaxUrl2(url,type);
	    }
	    else if(type == 1)
	    {
	   		url = "/modules/operatingStatistics/borrowOverdueAreaDistributed.html?type=" + type + "&date=" + date;
	   		ajaxUrl2(url,type);
	    }
    }
    
	var ajaxUrl2 = function (url,type){
		$.ajax({
			url:url + "&randomTime=" + (new Date()).getTime(),
			type:"get",
			dataType:"json",
			cache:false,
			success:function(data){
				var option;
				if(type == 0)//type0 饼图
				{
					var sums = 0;
					for(var i=0;i<data.arr.length;i++)
					{
						sums = sums+data.arr[i];
						if(sums == 0)
						{
							fuText = '暂无数据';
						}
						else
						{
							fuText = '';
						}
						if(data.arr[i] == 0)
						{
							delete data.arr[i];
						}
					}
					option1 = {
						title : {
						    text: '逾期借款人年龄分布',
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:['10-20岁','20-30岁','30-40岁','40-50岁','50岁以上']
					    },
					    calculable : true,
						tooltip : {
						    show: true
						},
						series : [
						    {
						        name:'年龄分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
						            {value:data.arr[0],name:'10-20岁'},
						            {value:data.arr[1],name:'20-30岁'},
						            {value:data.arr[2],name:'30-40岁'},
						            {value:data.arr[3],name:'40-50岁'},
						            {value:data.arr[4],name:'50岁以上'}
						        ]
						    }
						]
					}
					option2 = {
						title : {
						    text: '逾期借款人性别分布',
						    x : 'center',
					        y : 'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						calculable : true,
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:['男','女']
					    },
						tooltip : {
						    show: true
						},
						series : [
						    {
						        name:'性别分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
						            {value:data.arr[5],name:'男'},
						            {value:data.arr[6],name:'女'}
						        ]
						    }
						]
					}
					option3 = {
						title : {
						    text: '逾期借款人文化程度分布',
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	}
						},
						calculable : true,
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:['小学','初中','高中','大专','本科','研究生及以上']
					    },
						tooltip : {
						    show: true
						},
						series : [
						    {
						        name:'文化程度分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
						            {value:1,name:'小学'},
						            {value:13,name:'初中'},
						            {value:15,name:'高中'},
						            {value:7,name:'大专'},
						            {value:7,name:'本科'},
						            {value:7,name:'研究生及以上'}
						        ]
						    }
						]
					}
					option4 = {
						title : {
						    text: '逾期借款人婚姻状况分布',
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	}
						},
						calculable : true,
						legend: {
					        orient : 'vertical',
					        x : '80%',
					        y : 'center',
					        data:['未婚','已婚','离异']
					    },
						tooltip : {
						    show: true
						},
						series : [
						    {
						        name:'婚姻状况分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
						            {value:1,name:'未婚'},
						            {value:13,name:'已婚'},
						            {value:15,name:'离异'}
						        ]
						    }
						]
					}
					document.getElementById('investTableb').style.width = "48%";
					document.getElementById('investTablec').style.display = "inline-block";
					document.getElementById('investTabled').style.display = "inline-block";
					document.getElementById('investTablee').style.display = "inline-block";
					echarts.init(document.getElementById('investTableb')).setOption(option1); 
			        echarts.init(document.getElementById('investTablec')).setOption(option2); 
			        echarts.init(document.getElementById('investTabled')).setOption(option3); 
			        echarts.init(document.getElementById('investTablee')).setOption(option4); 
					                    
				}
				else if(type == 1)//type1 柱状图
				{
					var provinces = [],counts = [];
					var sums = 0;
					if(data.counts.length == 0)
					{
						counts.push('0');
						fuText = '暂无数据';
					}
					else
					{
						fuText = '';
					}
					option = {
					    title : {
					        text: '各省人数分布',
					        x:'center',
			            	subtext:fuText
					    },
					    tooltip : {
					    	show:true,
					        trigger: 'axis'
					    },
					    legend: {
					        orient: 'vertical',
					        x:'left',
					        data:['人数（人）']
					    },
					    dataRange: {
					        min: 0,
					        max: 25000,
					        x: 'left',
					        y: 'bottom',
					        text:['高','低'],           // 文本，默认为数值文本
					        calculable : true
					    },
					    roamController: {
					        show: true,
					        x: 'right',
					        mapTypeControl: {
					            'china': true
					        }
					    },
					    series : [
					        {
					            name: '人数（人）',
					            type: 'map',
					            mapType: 'china',
					            roam:true,
					            itemStyle:{
					                normal:{label:{show:true}},
					                emphasis:{label:{show:true}}
					            },
					            data:counts
					        }
					    ]
					};
			        document.getElementById('investTableb').style.width = "100%";
			        document.getElementById('investTablec').style.display = "none";
					document.getElementById('investTabled').style.display = "none";
					document.getElementById('investTablee').style.display = "none";
					echarts.init(document.getElementById('investTableb')).setOption(option); 
				}
			}
		});
	};

	var changBtn1 = $('.investorsTabs .tabs-header ul li a:eq(1)');
	changBtn1.click(function(){
		searchb();//初始化加载
	});
    $("#basicType,#basicYear,#basicMonth,#borrowType").change(function(){
    	searchb();
    });

});